﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Web.UI.WebControls;
//using www.SService;
using Controllerss;
using Core.Contract.DataContract;

namespace www.Admin
{
    public partial class ManagerUser : System.Web.UI.Page
    {
        #region Value
        private readonly ObjectCache _cache = MemoryCache.Default;
        //private readonly HyServicesClient _service1 = new HyServicesClient();
        private readonly StaffController _staffController=new StaffController();
        #endregion

        #region Contructor

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(Session["role"]) != 1)
                {
                    Message("Bạn không đủ quyền để thực hiện chức năng này.");
                    Response.Redirect("Default.aspx");
                }
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
            catch (Exception)
            {

            }
        }
        #endregion

        #region Event

        protected void grid_advertise_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grid_ManagerUser.PageIndex = e.NewPageIndex;
                LoadData();
            }
            catch (Exception)
            {

            }
        }

        protected void grid_advertise_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "cmd_grandrole")
                {
                    var id = e.CommandArgument;
                    Response.Redirect("GrandRole.aspx?id=" + id);
                }
            }
            catch (Exception)
            {

            }
        }

        #endregion

        #region Business

        private void LoadData()
        {
            if (_cache["Employee"] == null)
            {
                var lData = _staffController.GetStaffDataContracts();
                if (lData.Any())
                {
                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(27) };
                    _cache.Set("Employee", lData.ToList(), policy);
                }
            }
            var lData1 = (List<RelaxStaffDataContract>)_cache["Employee"];
            grid_ManagerUser.DataSource = lData1;
            grid_ManagerUser.DataBind();
        }

        private void Message(string str)
        {
            Response.Write("<script>alert('" + str + "');</script>");
        }
        #endregion

    }
}